package com.metaera.app.controller.core;


import com.metaera.app.controller.view.core.IPaginationView;
import com.metaera.app.data.model.BasicListResponse;
import com.metaera.app.data.rx.PaginationResponseObserver;
import com.metaera.app.data.rx.SchedulersTransformer;

import io.reactivex.Observable;

/**
 * 分页数据 帮助对象
 */
public abstract class SimplePaginationPresenter<T> extends RequestPresenter<IPaginationView<T>> implements
        IPaginationPresenter<IPaginationView<T>, T> {

    protected int mPage;

    @Override
    public void setModelAndView(IPaginationView<T> view) {
        super.setModelAndView(view);
    }

    @Override
    public void setPaginationView(IPaginationView<T> view) {
        // 占位
        setModelAndView(view);
    }

    @Override
    public void requestRefresh() {
        mPage = 1;

        try {
            execute().compose(SchedulersTransformer.transformer(mView))
                    .subscribe(new PaginationResponseObserver<>(this, mView, mPage));
        } catch (Exception e) {
            mView.failure(e);
        }
    }

    @Override
    public void requestLoadMore() {
        ++mPage;

        try {
            execute().compose(SchedulersTransformer.transformer(mView))
                    .subscribe(new PaginationResponseObserver<>(this, mView, mPage));
        } catch (Exception e) {
            mView.failure(e);
        }
    }

    @Override
    public void anewRequest() {
        requestRefresh();
    }

    protected abstract Observable<BasicListResponse<T>> execute();

}
